package com.cx.base.day26_序列化;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 序列化
 */
public class SerializableDemo {

    public static void main(String[] args) {

        File file = new File("d:\\file\\serializable_demo\\file2.txt");
//        if(file.isFile() && !file.exists()){
//            try {
//                file.createNewFile();
//            } catch (IOException e) {
//                System.out.println("文件创建失败"+e.getMessage());
//            }
//        }
        try {
            File parent = file.getParentFile();
            if(!parent.exists()){
                parent.mkdirs();
                file.createNewFile();
            }
        } catch (IOException e) {
            System.out.println("文件创建失败"+e.getMessage());
        }
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(file);
            ObjectOutputStream oos = null;
            try{
                oos = new ObjectOutputStream(fos);
                Pepole pepole = new Pepole("name","age");
                oos.writeObject(pepole);
                oos.flush();
            }catch (IOException e){
                try{
                    oos.close();
                }catch (IOException e2){
                    System.out.println("oss关闭失败"+e2.getMessage());
                }
            }
        }catch (IOException e){
            try{
                fos.close();
            }catch (IOException e4){
                System.out.println("fos关闭失败"+e4.getMessage());
            }
        }

    }
}
